package com.File.exer;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//图片的加密
public class PicTest {

    @Test
    public void test1() throws IOException {

        FileInputStream fileInputStream = new FileInputStream(new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG"));

        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian8.PNG"));

        byte[] bytes = new byte[1024];

        int len;
        while ((len = fileInputStream.read(bytes)) != -1){
            //字节数组进行修改
//            for(byte b : bytes){
//                b = (byte) (b ^ 5);
//            }
            //正确的
            for(int i = 0; i < len; i++){
                //在这里进行图片的加密，使用^号
                bytes[i] = (byte)(bytes[i] ^ 5);
            }

            fileOutputStream.write(bytes, 0, len);
        }

        fileInputStream.close();
        fileOutputStream.close();

    }


//    图片的解密
    @Test
    public void test2() throws IOException {

        FileInputStream fileInputStream = new FileInputStream(new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian8.PNG"));

        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian10.PNG"));

        byte[] bytes = new byte[1024];

        int len;
        while ((len = fileInputStream.read(bytes)) != -1){
            //字节数组进行修改
//            for(byte b : bytes){
//                b = (byte) (b ^ 5);
//            }
            //正确的
            for(int i = 0; i < len; i++){
                //在这里进行图片的解密 ^5回去
                bytes[i] = (byte)(bytes[i] ^ 5);
            }

            fileOutputStream.write(bytes, 0, len);
        }

        fileInputStream.close();
        fileOutputStream.close();

    }


}
